//
//  UIMarqueeBarView.m
//  sp
//
//  Created by 孙智慧 on 16/1/26.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "UIMarqueeBarView.h"


@implementation UIMarqueeBarView
BOOL can=NO;

- (id)initWithFrame:(CGRect)frame

{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        // Initialization code
        
        //[self setupView];
        
    }
    
    return self;
    
}



-(id)initWithCoder:(NSCoder *)aDecoder {
    
    if( (self = [super initWithCoder:aDecoder]) ) {
        
        // Initialization code
        
        //[self setupView];
        
    }
    
    return self;
    
}



- (void)setupView
{
    [self setClipsToBounds:YES];
    
    [self setOpaque:YES];
    
    UILabel *lblContent= [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
    
    [lblContent setText:self.musicName];
    
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:12];
    
    CGSize size = CGSizeMake(200,30);
    
    NSDictionary *attributes = @{NSFontAttributeName:font};
    
    CGSize labelsize = [self.musicName boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    
    CGRect oldFrame= CGRectMake(0,0, labelsize.width, labelsize.height);
    
    [lblContent setFrame:oldFrame];
    
    [lblContent setTextColor:[UIColor whiteColor]];
    
    [lblContent setBackgroundColor:[UIColor clearColor]];
    
    [lblContent setFont:font];
    
    [lblContent setNumberOfLines:1];
    
    [lblContent setOpaque:YES];
    
    [self addSubview:lblContent];
    
}



- (void)startM
{
    
    if (self.subviews.count == 0) {
        
        [self setupView];
        
    }
    UILabel *label=[self.subviews objectAtIndex:0];
    [label setText:self.musicName];
    
    //self.firstButton.backgroundColor = [UIColorgreenColor];
    
    CGRect frame = [self.subviews objectAtIndex:0] .frame;
    
    frame.origin.x = self.bounds.size.width;
    
    [self.subviews objectAtIndex:0].frame = frame;
    
    [UIView beginAnimations:@"testAnimation"context:NULL];
    
    [UIView setAnimationDuration:8.8f];
    
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    
    [UIView setAnimationDelegate:self];
    
    [UIView setAnimationRepeatAutoreverses:NO];
    
    [UIView setAnimationRepeatCount:999999];
    
    frame = [self.subviews objectAtIndex:0].frame;
    
    frame.origin.x = -frame.size.width;
    
    [self.subviews objectAtIndex:0].frame = frame;
    
    [UIView commitAnimations];
    
    
    //    can= YES;
    //    [self startAnimation];
    
}

- (void)stopM
{
    can= NO;
    [self initLable];
}

-(void) initLable
{
    if(self.subviews.count>0)
    {
        [[self.subviews objectAtIndex:0].layer removeAllAnimations];
        [[self.subviews objectAtIndex:0] removeFromSuperview];
    }
}


-(void)startAnimation
{
    
    [UIView beginAnimations:@"MarqueeBarAniamation" context:nil];
    
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    
    [UIView setAnimationDuration:5];
    
    [UIView setAnimationDelegate:self];
    
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    
    CGRect viewFrame = [self.subviews objectAtIndex:0].frame;
    
    viewFrame.origin.x = -320;
    
    [[self.subviews objectAtIndex:0] setFrame:viewFrame];
    
    [UIView commitAnimations];
    
}



-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    if(can== YES)
    {
        CGRect viewFrame = [self.subviews objectAtIndex:0].frame;
        
        viewFrame.origin.x = 320;
        
        [[self.subviews objectAtIndex:0] setFrame:viewFrame];
        if(can== YES)
            [self startAnimation];
    }
}
@end
